﻿using System.Collections.Generic;

namespace Chess.Domain
{
	internal class PawnPositionDeterminant : IPositionDeterminant
	{
		private Color _Color;

		/// <summary>
		/// 
		/// </summary>
		public Color Color
		{
			get { return _Color; }
			internal set { _Color = value; }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="position"></param>
		/// <returns></returns>
		public List<Position> AvailablePositions(Position currentPosition)
		{
			List<Position> results = new List<Position>();
			Position item;
			int deltaY = DetermineDirection(Color);

			// Position directly above
			if (currentPosition.CanTranslate(deltaY, 0, out item))
			{
				results.Add(item);
			}
			// Right capturing position
			if (currentPosition.CanTranslate(deltaY, 1, out item))
			{
				results.Add(item);
			}
			// Left capturing positon
			if (currentPosition.CanTranslate(deltaY, -1, out item))
			{
				results.Add(item);
			}
			// Position 2 spaces above when on second rank
			if (IsInitialPosition(Color, currentPosition))
			{
				if (currentPosition.CanTranslate(deltaY * 2, 0, out item))
				{
					results.Add(item);
				}
			}

			return results;
		}


		#region " Helper Methods "

		/// <summary>
		/// 
		/// </summary>
		/// <param name="Color"></param>
		/// <param name="position"></param>
		/// <returns></returns>
		private bool IsInitialPosition(Color color, Position position)
		{
			bool result = false;

			switch (color)
			{
				case Color.White:
					result = (position.Rank == Rank.R2);

					break;
				case Color.Black:
					result = (position.Rank == Rank.R7);

					break;
			}

			return result;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="color"></param>
		/// <returns></returns>
		private int DetermineDirection(Color color)
		{
			int result = 0;

			switch (color)
			{
				case Color.White:
					result = 1;

					break;
				case Color.Black:
					result = -1;

					break;
			}

			return result;
		} 

		#endregion
	
	}
}
